package controllers;
import models.Customer;
import models.Location;
import models.SolutionAdoption;
import models.SolutionSubmission;

import java.io.IOException;

import javax.servlet.http.*;

import com.googlecode.objectify.Objectify;
import com.googlecode.objectify.ObjectifyService;
import com.googlecode.objectify.Query;




@SuppressWarnings("serial")
public class ajxCustomerServlet extends HttpServlet {
	public void doGet(HttpServletRequest req, HttpServletResponse resp)
			throws IOException {
		resp.setContentType("text/xml");

	}
	
	
	
	
	public boolean Delete(String id)
	{
		try
		{
			String Name = "";
			Long key = Long.parseLong(id);
			
			Objectify ofy = ObjectifyService.begin();
			
			Query<Customer> q = ofy.query(Customer.class).filter("custID", key);
			for ( Customer f : q.fetch() ) {
				Name = f.getCustName();
				break;
			}
				
			Query<SolutionAdoption> Adoption = ofy.query(SolutionAdoption.class).filter("Customer", Name);
			Query<SolutionSubmission> Submission = ofy.query(SolutionSubmission .class).filter("Customer", Name);
			if (Adoption.countAll() == 0 && Submission.countAll() ==0 )
			{
				Customer customer = ofy.get(Customer.class, key);
				ofy.delete(customer);
				return true;
			
			}
			return false;
		}
		catch (Exception e) {
			// TODO: handle exception
			return false;
		}
	}
	
	public String GetCusname( Long id)
	{
		String result ="";
		Objectify ofy = ObjectifyService.begin();
		Query<Customer> qCount = ofy.query(Customer.class).filter("custID", id);
		if (qCount.countAll() > 0)
		{
			for ( Customer f : qCount.fetch() ) {
				result =f.getCustName();
				break;
			}
		}
		return result;
	}
}
